package com.obas.skin {
   import flash.display.GradientType;
   import mx.core.UIComponent;
   import mx.core.EdgeMetrics;
   import mx.skins.Border;
   import mx.styles.StyleManager;
   import mx.utils.ColorUtil;
   import mx.skins.halo.HaloColors;

   public class MenuBarCustomSkin extends Border {
      private static var cache:Object = {};

      private static function calcDerivedStyles(themeColor:uint,
                                                fillColor0:uint,
                                                fillColor1:uint):Object {
         var key:String = HaloColors.getCacheKey(themeColor, fillColor0, fillColor1);
         if (!cache[key]) {
            var o:Object = cache[key] = {};
            HaloColors.addHaloColors(o, themeColor, fillColor0, fillColor1);
         }
         return cache[key];
      }

      /**
       * Constructor.
       */
      public function MenuBarCustomSkin() {
         super();
      }

      private var _borderMetrics:EdgeMetrics = new EdgeMetrics(0, 0, 0, 0);
      override public function get borderMetrics():EdgeMetrics {
         return _borderMetrics;
      }

      override public function get measuredWidth():Number {
         return UIComponent.DEFAULT_MEASURED_MIN_WIDTH;
      }

      override public function get measuredHeight():Number {
         return UIComponent.DEFAULT_MEASURED_MIN_HEIGHT;
      }

      override protected function updateDisplayList(w:Number, h:Number):void {
         super.updateDisplayList(w, h);

         var bevel:Boolean = getStyle("bevel");
         var borderColor:uint = getStyle("borderColor");
         var cornerRadius:Number = getStyle("cornerRadius");
         var fillAlphas:Array = getStyle("fillAlphas");
         var fillColors:Array = getStyle("fillColors");
         StyleManager.getStyleManager(null).getColorNames(fillColors);
         var themeColor:uint = getStyle("themeColor");
         var derStyles:Object = calcDerivedStyles(themeColor, fillColors[0], fillColors[1]);

         var borderColorDrk1:Number =
            ColorUtil.adjustBrightness2(borderColor, -25);

         var cr:Number = Math.max(0, cornerRadius);
         var cr1:Number = Math.max(0, cornerRadius - 1);
         var upFillColors1:Array = [ fillColors[0], fillColors[1] ];
         var upFillAlphas1:Array = [ fillAlphas[0], fillAlphas[1] ];
         var upFillColors2:Array = [ fillColors[1], fillColors[0] ];
         var upFillAlphas2:Array = [ fillAlphas[1], fillAlphas[0] ];

         graphics.clear();
         drawRoundRect( 1, 1, w - 2, h/2 - 1, cr1, upFillColors1, upFillAlphas1, verticalGradientMatrix(1, 1, w - 2, h/2 - 1));
         drawRoundRect( 1, h/2, w - 2, h/2 - 1, cr1, upFillColors2, upFillAlphas2, verticalGradientMatrix(1, h/2, w - 2, h/2 - 1));
      }
   }
}